package com.wie.controller;

import com.wie.BaseContext.BaseContext;
import com.wie.domain.dto.*;
import com.wie.domain.entity.Result;
import com.wie.domain.entity.User;
import com.wie.domain.vo.LoginWithPasswordVO;
import com.wie.domain.vo.UserInfoVO;
import com.wie.properties.JwtProperties;
import com.wie.service.UserService;
import com.wie.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户注册
     * 发送邮箱验证码
     * @param signupDTO
     * @return
     */
    @PostMapping("/sendCode")
    public Result sendCode(@RequestBody SignupDTO signupDTO) {
        log.info("发送邮箱验证码：{}", signupDTO);
        userService.sendCode(signupDTO);
        return Result.success();
    }

    /**
     * 用户注册
     * 校验验证码
     * @param verifyDTO
     * @return
     */
    @PostMapping("/signup")
    public Result signup(@RequestBody VerifyDTO verifyDTO) {
        log.info("校验验证码并注册：{}", verifyDTO);
        userService.signup(verifyDTO);
        return Result.success();
    }

    /**
     * 使用账号密码登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login1")
    public Result loginWithPassword(@RequestBody LoginWithPasswordDTO loginDTO) {
        log.info("密码登录：{}", loginDTO.getUsername());
        // 校验用户
        User user = userService.login1(loginDTO);
        // 登录成功，生成令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSignKey(),
                jwtProperties.getExpire(),
                claims);
        LoginWithPasswordVO loginVO = new LoginWithPasswordVO().builder()
                .token(token)
                .build();

        return Result.success(loginVO);
    }

    /**
     * 使用邮箱登录
     * 发送验证码
     * @param loginDTO
     * @return
     */
    @PostMapping("/login2")
    public Result loginWithEmail1(@RequestBody LoginWithEmailDTO loginDTO) {
        log.info("邮箱登录: {}", loginDTO.getEmail());
        userService.login2(loginDTO);
        return Result.success();
    }

    /**
     * 用户使用邮箱登录
     * 校验验证码并生成 token
     * @param loginDTO
     * @return
     */
    @PostMapping("/login3")
    public Result loginWithEmail2(@RequestBody LoginWithEmailDTO loginDTO) {
        log.info("使用邮箱登录: {}", loginDTO);
        // 校验验证码是否正确
        User user = userService.login3(loginDTO);
        // 登录成功，生成令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSignKey(),
                jwtProperties.getExpire(),
                claims);
        LoginWithPasswordVO loginVO = new LoginWithPasswordVO().builder()
                .token(token)
                .build();
        return Result.success(loginVO);
    }

    /**
     * 删除用户
     * @param deleteUserDTO
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody DeleteUserDTO deleteUserDTO) {
        log.info("删除用户，{}", deleteUserDTO.getUsername());
        userService.delete(deleteUserDTO.getUsername());
        return Result.success();
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/info")
    public Result get() {
        log.info("获取当前用户信息");
        UserInfoVO userInfoVO = userService.getUserInfo();
        return Result.success(userInfoVO);
    }

    /**
     * 更新用户信息
     * @param updateUserInfoDTO
     * @return
     */
    @PostMapping("/updateInfo")
    public Result updateUserInfo(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
        Integer userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);
        log.info("当前用户为: {}", user.getUsername());
        log.info("更新用户信息：{}", updateUserInfoDTO);
        userService.updateUserInfo(updateUserInfoDTO);
        return Result.success();
    }

    /**
     * 改变用户的单点登录
     * @return
     */
    @PostMapping("/updateOnly")
    public Result updateOnly() {
        log.info("改变用户的单点登录");
        userService.updateOnly();
        return Result.success();
    }

    /**
     * 更新用户密码
     * @param updatePassDTO
     * @return
     */
    @PostMapping("/updatePass")
    public Result updatePassword(@RequestBody UpdatePassDTO updatePassDTO) {
        log.info("更新用户密码");
        userService.updatePass(updatePassDTO);
        return Result.success();
    }

}
